package com.shuwen.gcdj.service.dictionary.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuwen.gcdj.bean.dictionary.request.DictionaryAddParam;
import com.shuwen.gcdj.bean.dictionary.request.DictionaryListParam;
import com.shuwen.gcdj.bean.dictionary.request.DictionaryMainParam;
import com.shuwen.gcdj.bean.dictionary.request.DictionaryUpdateParam;
import com.shuwen.gcdj.bean.dictionary.response.DictionaryDetailVO;
import com.shuwen.gcdj.bean.dictionary.response.DictionaryMainVO;
import com.shuwen.gcdj.bean.dictionaryItem.response.DictionaryItemMainVO;
import com.shuwen.gcdj.common.api.CommonPage;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.common.api.ResultCode;
import com.shuwen.gcdj.common.exception.ApiException;
import com.shuwen.gcdj.dto.account.InitUserDto;
import com.shuwen.gcdj.entity.Dictionary;
import com.shuwen.gcdj.entity.DictionaryItem;
import com.shuwen.gcdj.mapper.mysql.DictionaryMapper;
import com.shuwen.gcdj.service.dictionary.DictionaryService;
import com.shuwen.gcdj.service.dictionaryItem.DictionaryItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Autowired
    private DictionaryItemService dictionaryItemService;

    @Override
    public CommonPage<DictionaryMainVO> getPages(Page<Dictionary> page, DictionaryMainParam param) {

        IPage<DictionaryMainVO> pages = dictionaryMapper.getPageList(page, param);
        return CommonPage.restPage(pages);

    }

    @Override
    public  CommonResult<List<DictionaryDetailVO>> getList(DictionaryListParam param) {

        // 获取数据字典列表
        List<DictionaryMainVO> dictionaryMainVOList = dictionaryMapper.getList(param);
        List<DictionaryDetailVO> dictionaryDetailVOList = new ArrayList<>();
        // 查询结果为空直接返回
        if (dictionaryMainVOList.isEmpty()) {
            return CommonResult.failed("");
        }
        // 获取数据字典ID列表
        List<String> dictionaryIds = dictionaryMainVOList.stream().map(DictionaryMainVO::getId).collect(Collectors.toList());
        // 通过数据字典ID查询选项
        List<DictionaryItem> dictionaryItemList = dictionaryItemService.list(
                new LambdaQueryWrapper<DictionaryItem>().in(DictionaryItem::getDictionaryId, dictionaryIds)
        );
        List<DictionaryItemMainVO> dictionaryItemMainVOList = new ArrayList<>();
        // 字典选项数据转换
        if (!dictionaryItemList.isEmpty()) {
            for (DictionaryItem dictionaryItem : dictionaryItemList) {
                DictionaryItemMainVO dictionaryItemMainVO = new DictionaryItemMainVO();
                BeanUtils.copyProperties(dictionaryItem, dictionaryItemMainVO);
                dictionaryItemMainVOList.add(dictionaryItemMainVO);
            }
        }
        // 组合数据字典和对应选项
        for (DictionaryMainVO dictionaryMainVO : dictionaryMainVOList) {
            DictionaryDetailVO dictionaryDetailVO = new DictionaryDetailVO();
            BeanUtils.copyProperties(dictionaryMainVO, dictionaryDetailVO);
            List<DictionaryItemMainVO> items = dictionaryItemMainVOList.stream().filter(item -> item.getDictionaryId().equals(dictionaryMainVO.getId())).collect(Collectors.toList());
            dictionaryDetailVO.setItems(items);
            dictionaryDetailVOList.add(dictionaryDetailVO);
        }
        return CommonResult.success(dictionaryDetailVOList);

    }

    @Override
    public CommonResult<DictionaryDetailVO> getDetail(String id) {
        // 查询数据字典
        Dictionary dictionary = getById(id);
        if (dictionary == null) {
            throw new ApiException(ResultCode.VALIDATE_FAILED);
        }
        // 通过数据字典ID查询选项列表
        List<DictionaryItem> dictionaryItemList = dictionaryItemService.list(
                new LambdaQueryWrapper<DictionaryItem>().eq(DictionaryItem::getDictionaryId, dictionary.getId())
        );
        List<DictionaryItemMainVO> dictionaryItemMainVOList = new ArrayList<>();
        // 字典选项数据转换
        if (!dictionaryItemList.isEmpty()) {
            for (DictionaryItem dictionaryItem : dictionaryItemList) {
                DictionaryItemMainVO dictionaryItemMainVO = new DictionaryItemMainVO();
                BeanUtils.copyProperties(dictionaryItem, dictionaryItemMainVO);
                dictionaryItemMainVOList.add(dictionaryItemMainVO);
            }
        }
        // 组合数据字典和选项
        DictionaryDetailVO dictionaryDetailVO = new DictionaryDetailVO();
        BeanUtils.copyProperties(dictionary, dictionaryDetailVO);
        dictionaryDetailVO.setItems(dictionaryItemMainVOList);
        return CommonResult.success(dictionaryDetailVO);

    }

    @Override
    public CommonResult<String> add(DictionaryAddParam param, InitUserDto req){
        Dictionary dictionary = new Dictionary();
        dictionary.setUpdateId(req.getUserId());
        dictionary.setCreateId(req.getUserId());
        dictionary.setCreateDeptId(req.getDeptId());
        BeanUtils.copyProperties(param,dictionary);
        dictionaryMapper.insert(dictionary);
        return CommonResult.success("");
    }

    @Override
    public CommonResult<String> update(DictionaryUpdateParam param, InitUserDto req){
        Dictionary dictionary = dictionaryMapper.selectById(param.getId());
        dictionary.setUpdateId(req.getUserId());
        dictionary.setUpdateTime(null);
        BeanUtils.copyProperties(param,dictionary);
        dictionaryMapper.update(dictionary,new UpdateWrapper<Dictionary>().lambda().eq(Dictionary::getId,param.getId()));
        return CommonResult.success("");
    }

    @Override
    public CommonResult<String> delete(String ids, InitUserDto req) throws ApiException{
        String[]id=ids.split(",");
        for (String s : id) {
            dictionaryMapper.update(null,new UpdateWrapper<Dictionary>().lambda()
                    .eq(Dictionary::getId,s)
                    .set(Dictionary::getIsDelete,1)
                    .set(Dictionary::getUpdateId,req.getUserId())
            );
        }
        return CommonResult.success("");
    }

}
